
             
/**
 * Copyright (c) 2012-2013 JLLT Corp. 2012-2013,All Rights Reserved.
 * This software is published under the 金麟龙腾商贸有限公司.
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 *
 * @File name:  TOrderDao.java 
 * @Create on:  2012-10-09 11:34:672
 * @Author   :  陈伟
 *
 * @ChangeList
 * ---------------------------------------------------
 * Date         Editor              ChangeReasons
 *
 */
           
package com.boho.ecp.order.dao;

            
import java.sql.Connection;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import cn.boho.framework.po.CQuery;
import cn.boho.framework.po.CQueryFactoryTool;
import cn.boho.framework.po.ComboPager;
import cn.boho.framework.po.DAO;
import cn.boho.framework.po.DynaBeanMap;
import cn.boho.framework.po.Pager;
            
/**
 * 【订单信息:T_ORDER】的Dao操作类
 * 
 */
public class TOrderDao extends DAO {
	/**
	* 根据会员ID分页查询所有【订单信息:T_ORDER】信息
	* @param con
	* @param pager
	* @return
	* @throws Exception
	* @date 2012-10-09
	* @author 陈伟
	*
	*/
	public static ComboPager queryTOrderPagerByMemberId(Connection con,Pager pager, Integer memberId, String productName,
			int status, Integer date)throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("select * from T_ORDER where IS_VALID = 1 and MEMBER_ID = ?");
		CQuery query = CQueryFactoryTool.createFactory().createCQuery();
		int i = 0;
		query.setInt(++i, memberId);
		if(StringUtils.isNotEmpty(productName)){
			sql.append(" and lower(PRODUCT) like ?");
			query.setString(++i, "%" + productName.toLowerCase() + "%");
		}
		if(status != 0){
			sql.append(" and STATUS = ?");
			query.setInt(++i, status);
		}
		if(date == 1){
			sql.append(" and DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= DATE(ORDER_DATE)");
		}
		sql.append(" order by ORDER_DATE desc");
		
		query.setCommand(sql.toString());
		return query.getDynaBeanMapComboPager("T_ORDER", pager,con);
	}
	
	/**
	* 根据主键【订单编号:ORDER_ID|】获得信息
	* @param con
	* @param orderId 订单编号
	* @return
	* @throws Exception
	* @date 2012-10-09
	* @author 陈伟
	*
	*/
	public static DynaBeanMap getTOrderByPk(Connection con,java.lang.String orderId, Integer memberId)throws Exception{
		DynaBeanMap dynaBeanMap = null;
		StringBuffer sql = new StringBuffer();
		sql.append("select * from T_ORDER where  ORDER_ID = ? and MEMBER_ID = ? and IS_VALID = 1");
		CQuery query = CQueryFactoryTool.createFactory().createCQuery();
		query.setCommand(sql.toString());
		query.setString(1,orderId);
		query.setInt(2, memberId);
		dynaBeanMap = query.getDynaBeanMap("T_ORDER", con);
		return dynaBeanMap;
	}
	
	/**
	 * 根据ID列表查询订单详细信息
	 * @param con
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	public static List queryOrderDetailList(Connection con, String ids) throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("select * from T_ORDER_DETAIL where IS_VALID = 1 and ORDER_ID in (" + ids + ")");
		CQuery query = CQueryFactoryTool.createFactory().createCQuery();
		query.setCommand(sql.toString());
		return query.getDynaBeanMapList("T_ORDER_DETAIL", con);
	}
	
	/**
	 * 根据ID查询订单详细信息
	 * @param con
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	public static List getOrderDetailById(Connection con, String id) throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("select * from T_ORDER_DETAIL where IS_VALID = 1 and ORDER_ID = ?");
		CQuery query = CQueryFactoryTool.createFactory().createCQuery();
		query.setString(1, id);
		query.setCommand(sql.toString());
		return query.getDynaBeanMapList("T_ORDER_DETAIL", con);
	}
	
	/**
	 * 根据订单ID查询订单状态变更历史
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public static List queryOrderLogList(Connection con, String id) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from T_ORDER_CHANGE_LOG where IS_VALID = 1 and ORDER_ID = ? order by CHANGE_DATE");
		CQuery query = CQueryFactoryTool.createFactory().createCQuery();
		query.setString(1, id);
		query.setCommand(sql.toString());
		return query.getDynaBeanMapList("T_ORDER_CHANGE_LOG", con);
	}
	
}
